package com.five.admin.controller;

import com.five.admin.service.ArticleListService;
import com.five.controller.BaseController;
import com.five.entity.CourseClassify;
import com.five.entity.qo.ArticleQo;
import com.five.entity.qo.CourseQo;
import com.five.vo.BaseResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：vizi
 * @date ：Created in 2021/6/17 17:56
 */
@Controller
@RequestMapping("article")
@Api(tags = "文章列表接口")
@CrossOrigin(allowCredentials = "true",originPatterns = "*")
public class ArticleListController extends BaseController {

    @Autowired
    private ArticleListService articleListService;

    /**
     * @return文章列表【分页数据】
     */
    @ResponseBody
    @RequestMapping(value = "getArticle",method = RequestMethod.GET)
    @ApiOperation("获取文章列表")
    public BaseResultVO getCourseClassify(String name,Integer page){
        System.out.println(name);
        List<ArticleQo> list = articleListService.fuzzyQueryByTypename(name, page);
        System.out.println(list);
        int pages = ((articleListService.countArticle(name)-1)/10)+1;
        Map<String,Object> resultMap = new HashMap();
        resultMap.put("pages",pages);
        resultMap.put("articleList",list);
        return successResp(resultMap);
    }

    /**
     * @return 文章列表展示
     */
    @ResponseBody
    @RequestMapping(value = "articleList",method = RequestMethod.GET)
    @ApiOperation("文章列表接口")
    public BaseResultVO articleList(String name){
        List<ArticleQo> list = articleListService.selectArticleList();
        System.out.println(list);
        int pages = ((articleListService.countArticle(name)-1)/10)+1;
        Map<String,Object> resultMap = new HashMap();
        resultMap.put("pages",pages);
        resultMap.put("articleList",list);
        return successResp(resultMap);
    }

    @ResponseBody
    @RequestMapping(value = "findById",method = RequestMethod.POST)
    @ApiOperation("根据id查询文章信息")
    public BaseResultVO findById(long id){
        ArticleQo article = articleListService.findById(id);
        return successResp(article);
    }

    @ResponseBody
    @RequestMapping(value = "deleteById",method = RequestMethod.POST)
    @ApiOperation("根据id删除课程类别")
    public BaseResultVO deleteById(long id){
        articleListService.deleteArticle(id);
        return successRespWithMessage("删除成功");
    }

    @ResponseBody
    @RequestMapping(value = "deleteSelect",method = RequestMethod.POST)
    @ApiOperation("删除选中的课程类别")
    public BaseResultVO deleteSelect(@RequestParam(value = "ids[]") long[] ids){
        articleListService.deleteSelectArticle(ids);
        return successRespWithMessage("删除成功");
    }

    /**
     * @return课程类型列表
     */
    @ResponseBody
    @RequestMapping(value = "fuzzyQueryArticle",method = RequestMethod.GET)
    @ApiOperation("模糊查询课程类型")
    public BaseResultVO fuzzyQueryCourseClassify(String name,int page){
        List<ArticleQo> list = articleListService.fuzzyQueryByTypename(name, page);
        return successResp(list);
    }

}
